package blue_bridge_cup_learning.data_struct_code;

public class Stack {
    // 栈的容量
    private int maxSize;
    // 栈顶指针
    private int top;
    // 栈
    private int[] stack;

    public Stack(int maxSize) {
        this.maxSize = maxSize;
        this.top = -1;
        this.stack = new int[maxSize];
    }

    public boolean isEmpty() {
        return this.top == -1;
    }

    public boolean isFull() {
        return this.top == this.maxSize - 1;
    }

    public void Push(int value) {
        if (this.isFull()) {
            System.out.println("栈已满，无法添加元素");
            return;
        }
        this.top++;
        this.stack[this.top] = value;
    }

    public int Pop() {
        if (this.isEmpty()) {
            System.out.println("栈为空,请先添加元素");
            return -1;
        }
        int value = this.stack[this.top];
        this.top--;
        return value;
    }

    public void printStack() {
        for (int i = this.top; i >= 0; i--) {
            System.out.println(this.stack[i]);
        }
    }

    public static void main(String[] args) {
        Stack stack = new Stack(5);
        stack.Push(1);
        stack.Push(2);
        stack.Push(3);
        stack.printStack();
        System.out.println("出栈");
        System.out.println(stack.Pop());
        stack.printStack();
        System.out.println("再出栈");
        System.out.println(stack.Pop());
        stack.printStack();
        System.out.println("-------------");
        stack.Push(4);
        stack.Push(5);
        stack.printStack();
    }
}
